package didi.hcp.questionrobot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import didi.hcp.questionrobot.bo.SopBO;
import didi.hcp.questionrobot.common.QueryRequest;
import didi.hcp.questionrobot.common.RobotConstant;
import didi.hcp.questionrobot.entity.Sop;
import didi.hcp.questionrobot.mapper.SopMapper;
import didi.hcp.questionrobot.service.ISopService;
import didi.hcp.questionrobot.utils.SortUtil;
import didi.hcp.questionrobot.vo.SopVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * Service实现
 *
 * @author huchengpeng
 * @date 2021-12-11 16:37:35
 */
@Service("sopService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SopServiceImpl extends ServiceImpl<SopMapper, Sop> implements ISopService {

    @Autowired
    private SopMapper sopMapper;

    @Override
    public boolean addSop(SopBO sopBO) {
        final Sop sop = new Sop();
        BeanUtils.copyProperties(sopBO,sop);
        final Date now = new Date();
        sop.setCreateTime(now);
        sop.setUpdateTime(now);
        final boolean success = save(sop);
        return success;
    }

    @Override
    public boolean updateSop(SopBO sopBO) {
        final Sop sop = new Sop();
        BeanUtils.copyProperties(sopBO,sop);
        final boolean success = updateById(sop);
        return success;
    }

    @Override
    public boolean deprecatedOrStartSop(SopBO sopBO) {
        final Sop sop = new Sop();
        sop.setId(sopBO.getId());
        sop.setIsDelete(sopBO.getIsDelete());
        final boolean success = updateById(sop);
        return success;
    }

    @Override
    public IPage<Sop> findSops(QueryRequest request, SopBO sopBO) {
        QueryWrapper<Sop> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(sopBO.getTitle()),"title",sopBO.getTitle())
                .eq(sopBO.getCategoryId()!=null,"category_id",sopBO.getCategoryId());
        Page<Sop> page = new Page<>(request.getPageNum(), request.getPageSize());
        //排序规则设置，使用'#'分隔不同的参数，从左往右的优先级排序
        SortUtil.handlePageSort(request, page, "updateTime#createTime", RobotConstant.ORDER_DESC, true);
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Sop> getAllSop() {
        QueryWrapper<Sop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        return list(queryWrapper);
    }
}
